﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class AccountTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("username", "email@example.com", "password")]
        public void Can_CRUD(string username, string email, string password)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;

            #endregion

            var account = Processor.Execute(new CreateAccount
            {
                Email = email,
                Username = username,
                Password = password,
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;

            Assert.NotNull(account);
            account.Username.AreEqualIgnoringCase(username);
            account.Email.AreEqualIgnoringCase(email);
            account.Flags.AreEqual(AccountFlags.None);
            account.AreEqual(Processor.Execute(new GetAccount
            {
                EntityID = account.EntityID
            }).Result);
            account.AreEqual(Processor.Execute(new GetAccountByEmail
            {
                Email = email
            }).Result);
            account.AreEqual(Processor.Execute(new GetAccountByUsername
            {
                Username = username
            }).Result);
            account.AreEqual(Processor.Execute(new GetAccountByGuid
            {
                Guid = account.Guid
            }).Result);

            username = username.Reverse();
            email = "upd_" + email;
            var upd_account = Processor.Execute(new UpdateAccount
            {
                EntityID = account.EntityID,
                Email = email,
                Username = username,
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;

            Assert.NotNull(upd_account);
            upd_account.AreEqual(account);
            upd_account.Username.AreEqualIgnoringCase(username);
            upd_account.Email.AreEqualIgnoringCase(email);

            Processor.Execute(new DeleteAccount
            {
                EntityID = account.EntityID
            });
        }

        [TestCase("username", "email", "")]
        [TestCase("username", "", "password")]
        [TestCase("", "email", "password")]
        public void Cannot_Create_If_UsernameOrEmailOrPassword_Empty(string username, string email, string password)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateAccount
            {
                Email = email,
                Username = username,
                Password = password
            }));
        }

        [TestCase("username", "")]
        [TestCase("", "email")]
        public void Cannot_Update_If_UsernameOrEmail_Empty(string username, string email)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateAccount
            {
                Email = email,
                Username = username
            }));
        }

        [Test]
        public void Can_Manage_Role_Relations()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;

            #endregion

            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var role = Processor.Execute(new CreateRole
            {
                Code = "xy"
            }).Result;

            Processor.Execute(new MapAccountToRole
            {
                EntityID = account.EntityID,
                ChildID = role.EntityID
            }).Result.Roles
            .DoesContain(role);

            Processor.Execute(new UnmapAccountFromRole
            {
                EntityID = account.EntityID,
                ChildID = role.EntityID
            }).Result.Roles
            .DoesNotContain(role);
        }

        [Test]
        public void Can_Pass_Verification()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;

            #endregion

            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID,
                Flags = AccountFlags.Active | AccountFlags.Verified
            }).Result;

            Processor.Execute(new InitAccountVerification
            {
                EntityID = account.EntityID
            }).Result.Flags.HasFlag(AccountFlags.Verified)
            .IsFalse();

            var verificationCode = Processor.Execute(new GetAccountAttributeValueByKey
            {
                HolderID = account.EntityID,
                Key = Par.In("Verification").Name("Code")
            }).ResultAs<Guid>();
            verificationCode.AreNotEqual(Guid.Empty);

            Processor.Execute(new CommitAccountVerification
            {
                EntityID = account.EntityID,
                VerificationCode = verificationCode
            }).Result.Flags.HasFlag(AccountFlags.Verified)
            .IsTrue();
        }
    }
}
